var canvas;
var ctx;
var channels;
var rate;
var frameBufferLength;

function audioMetadata(audio) {
	//var audio = document.getElementById(elementId);
	
	channels = audio.mozChannels;
	rate = audio.mozSampleRate;
	frameBufferLength = audio.mozFrameBufferLength;
};

function audioPlayed(event) {
	var samples = event.frameBuffer;
	var time = event.time;

	clearCanvas()

	for ( var i = 0; i < samples.length; i++) {
		var up = samples[i] * 100;
		ctx.fillRect(i, 50, 1, up);
	}
	
	setTimeout(clearCanvas, player.bpm);
};

function clearCanvas() {
	ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function registerAudioCallback(audio) {
	//var audio = document.getElementById(elementId);
	audio.addEventListener('MozAudioAvailable', audioPlayed, false);
	audio.addEventListener('loadedmetadata', function() {
		audioMetadata(audio);
	}, false);
};

function initVisualizer() {
	canvas = document.getElementById('visualizer');
	ctx = canvas.getContext('2d');
	ctx.fillStyle = "black";
};